﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game
{
    class ThrowPick : GameScreen
    {
        #region Declarations

        int hit = 0;
        int maxCount = 2;
        int numOfEnemy = 0;
        bool isThrow = false;

        Enemy[] enemy = new Enemy[8];
        Pick pick = new Pick();
        List<Enemy> enemyList = new List<Enemy>();
        Random rand = new Random();

        Texture2D background;
        Texture2D EnemyTexture;
        Texture2D PickTexture;
        Texture2D UserTexture;
        Texture2D BarTexture;

        #endregion

        #region Load

        public ThrowPick()
        {
            enemy[0] = new Enemy(new Rectangle(0, 0, 100, 100));
            enemy[1] = new Enemy(new Rectangle(100, 0, 100, 100));
            enemy[2] = new Enemy(new Rectangle(200, 0, 100, 100));
            enemy[3] = new Enemy(new Rectangle(300, 0, 100, 100));
            enemy[4] = new Enemy(new Rectangle(400, 0, 100, 100));
            enemy[5] = new Enemy(new Rectangle(500, 0, 100, 100));
            enemy[6] = new Enemy(new Rectangle(600, 0, 100, 100));
            enemy[7] = new Enemy(new Rectangle(700, 0, 100, 100));

            foreach (Enemy e in enemy)
            {
                e.Difficulty = 3;
            }
        }

        bool IsRegionTransparent(Texture2D texture, Rectangle r)
        {
            if (r.X + r.Width > texture.Width)
            {
                r.Width = texture.Width - r.X;
            }
            if (r.Y + r.Height > texture.Height)
            {
                r.Height = texture.Height - r.Y;
            }

            int size = r.Width * r.Height;
            Color[] buffer = new Color[size];

            texture.GetData<Color>(0, r, buffer, 0, size);
            return buffer.All(c => c == Color.Transparent);
        }

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            background = content.Load<Texture2D>("Texture/blank");
            EnemyTexture = content.Load<Texture2D>("Texture/Pick/Enemy");
            PickTexture = content.Load<Texture2D>("Texture/Pick/Pick");
            UserTexture = content.Load<Texture2D>("Texture/Pick/Point");
            BarTexture = content.Load<Texture2D>("Texture/Pick/Bar");
        }

        #endregion

        public override void Update(Touch touch, GameTime gameTime)
        {
            for (int i = numOfEnemy; i < maxCount; i++)
            {
                int num = rand.Next(0, 8);

                if (enemyList.Contains(enemy[num]))
                {
                    i--;
                }
                else
                {
                    enemyList.Add(enemy[num]);
                    numOfEnemy++;
                }
            }

            for (int i = enemyList.Count() - 1; i >= 0; i--)
            {
                enemyList[i].Update();

                if (enemyList[i].Position.Y > 330)
                {
                    enemyList[i].Initialize();
                    enemyList.RemoveAt(i);
                    numOfEnemy--;
                }
            }

            HandleInput(touch, gameTime);

            if (isThrow)
            {
                pick.Update();

                if (pick.Position.Y < 0)
                {
                    pick.Initialize();
                    isThrow = false;
                }
            }

            for (int i = enemyList.Count() - 1; i >= 0; i--)
            {
                if (enemyList[i].Position.Intersects(pick.Position))
                {
                    hit++;
                    numOfEnemy--;
                    enemyList[i].Initialize();
                    enemyList.RemoveAt(i);
                    isThrow = false;
                    pick.Initialize();
                }
            }
        }

        public void HandleInput(Touch touch, GameTime gameTime)
        {
            foreach (GestureSample gesture in touch.Gestures)
            {
                if (gesture.GestureType == GestureType.Tap)
                {
                    Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y);

                    isThrow = true;
                    pick.Update(tapLocation);

                    for (int i = enemyList.Count() - 1; i >= 0; i--)
                    {
                        if (tapLocation.X > enemyList[i].Position.X &&
                            tapLocation.X < enemyList[i].Position.X + 100 &&
                            tapLocation.Y > enemyList[i].Position.Y &&
                            tapLocation.Y < enemyList[i].Position.Y + 100)
                        {
                            //hit++;
                            //numOfEnemy--;
                            //enemyList[i].Initialize();
                            //enemyList.RemoveAt(i);
                        }
                    }

                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
            spriteBatch.Draw(BarTexture, new Vector2(0, 430), Color.White);

            foreach (Enemy e in enemyList)
            {
                spriteBatch.Draw(EnemyTexture, e.Position, Color.White);
            }

            spriteBatch.Draw(PickTexture, pick.Position, Color.White);
        }
    }


}
